import java.util.HashMap;
import java.util.Map;

/**
 * Created by qtbui on 2017/9/20.
 */
class Solution
{
    private static Map<Integer,String> m_Data=new HashMap<Integer,String>();

    static
    {
       m_Data.put(1,"1");
    }

    public String countAndSay(int n)
    {
        if(m_Data.containsKey(n))
        {
            return m_Data.get(n);
        }

        String r = countAndSay(n-1);
        byte[] rBytes= r.getBytes();
        byte tmp = rBytes[0];
        int count=1;
        String newStr="";
        for(int i=1;i<rBytes.length;++i)
        {
            if(rBytes[i]==tmp)
            {
                count++;
            }
            else
            {
                newStr+=String.valueOf(count)+String.valueOf(tmp-0x30);

                tmp=rBytes[i];
                count=1;
            }
        }

        newStr+=String.valueOf(count)+String.valueOf(tmp-0x30);
        m_Data.put(n,newStr);

        return newStr;
    }
}

public class test
{
    public static void main(String[] args)
    {
        String r = new Solution().countAndSay(2);
        System.out.println(r);
    }
}
